/* include/kernel/mm.h 
 * 
 * This file is part of xnix. 
 * 
 * XNIX is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * XNIX is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with XNIX. If not, see <https://www.gnu.org/licenses/>
 */ 




#ifndef MM_H
#define MM_H


#ifndef __ASSEMBLER__

#include <sys/types.h>

#include <kernel/archinfo.h>
#include <kernel/klist.h>


/* this should be initiallized in arch_mm_init because 
 * mm_init uses this. */

extern __archptr_t total_ram;


/* bitmap-based memory pool. 
 * these are saving in one page. */

struct mempool_bmb {
	__uint32_t objsize;
	__uint32_t objsz_order;
	__uint32_t nobjs;

	void *bm;
	void *entry_addr;
};

extern struct mempool_bmb *klist_node_pool;


#define NRBUDDYLISTS 	9

/* buddy_pages	[0]: 	4k	(PAGESIZE >> 0)
 * 		[1]: 	8k	(PAGESIZE >> 1)
 * 		[2]: 	16k	(PAGESIZE >> 2)
 * 		[3]: 	32k	(PAGESIZE >> 3)
 * 		[4]: 	64k	(PAGESIZE >> 4)
 * 		[5]: 	128k 	(PAGESIZE >> 5)
 * 		[6]: 	256k 	(PAGESIZE >> 6)
 * 		[7]: 	512k 	(PAGESIZE >> 7)
 * 		[8]: 	1m	(PAGESIZE >> 8)
 * 		[9]: 	2m 	(PAGESIZE >> 9)
 * */

/* we keep these pages at boot-time in every list. 
 *
 * so at the boot-time (kmalloc and every malloc functions 
 * are not ready), we initial 2 page-frames in every lists. 
 * */

#define NPAGES_PER_LIST 4

#define BUDDY_FREE 	1
#define BUDDY_USING 	2

extern struct klist_node buddy_pages[NRBUDDYLISTS];

/* &_end is the end address of kernel */

extern void *_end;


void *getfreepages(int order, int gfp_flag);
int freepage(int order, void *);

struct mempool_bmb *mmp_bmb_create(__uint32_t objsize, __uint32_t objsize_order);
void *mmp_bmb_alloc(struct mempool_bmb *);
int mmp_bmb_free(void *);


#endif

#define GFP_KERNEL 	1


#endif

